/** 
 *  @Description:
 *  @ClassName  :com.hmsm.sys.action.PublishAction.java
 *  @Author     :yaosq
 *  @CreateDate :2013-7-2 下午05:34:19  
 **/
package com.hmsm.sys.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.hmsm.server.base.BaseAction;
import com.hmsm.server.util.QueryResult;
import com.hmsm.server.util.Util;
import com.hmsm.sys.bean.SysMessageBean;
import com.hmsm.sys.po.SysMessagePublish;
import com.hmsm.sys.po.UserInfo;
import com.hmsm.sys.service.SysMessagePublishInfoService;

/**
 * @Description:消息发布action类
 * @ClassName :com.hmsm.sys.action.PublishAction.java
 * @Author :yaosq
 * @CreateDate :2013-7-2 下午05:34:19
 **/

public class SysMessageAction extends BaseAction<UserInfo> {

	/**
	 * @since Ver 1.1
	 */
	
	private static final long serialVersionUID = -7450610371853475110L;
	
	@Resource
	private SysMessagePublishInfoService sysMessagePublishInfoService;
	
	public SysMessagePublishInfoService getSysMessagePublishInfoService() {
		return sysMessagePublishInfoService;
	}

	public void setSysMessagePublishInfoService(SysMessagePublishInfoService sysMessagePublishInfoService) {
		this.sysMessagePublishInfoService = sysMessagePublishInfoService;
	}

	private String nowpage = null;// 页码
	private String maxpage = null;// 每页显示条数
	private int count;
	private int allPages;
	private int page;// 当前页
	private int rowsPage;// 每页数量
	private QueryResult<SysMessagePublish> result;// 日志集合
	private int mid;
	private String title;// 发布标题
	private String messPublishDate;// 发布日期
	private String content;// 发布内容
	private String creater;// 创建者
	private String currentUnit;// 所属部门
	private String startTime;// 开始时间
	private String endTime;// 结束时间

	private SysMessagePublish sysMessagePublish;
	private List<SysMessageBean> sysMessagePublishList;
	private int type;
	private boolean isFirstLoginNowDay;
	private int userId;

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getMid() {
		return mid;
	}

	public void setMid(int mid) {
		this.mid = mid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMessPublishDate() {
		return messPublishDate;
	}

	public void setMessPublishDate(String messPublishDate) {
		this.messPublishDate = messPublishDate;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getCreater() {
		return creater;
	}

	public void setCreater(String creater) {
		this.creater = creater;
	}

	public String getCurrentUnit() {
		return currentUnit;
	}

	public void setCurrentUnit(String currentUnit) {
		this.currentUnit = currentUnit;
	}

	public SysMessagePublish getSysMessagePublish() {
		return sysMessagePublish;
	}

	public void setSysMessagePublish(SysMessagePublish sysMessagePublish) {
		this.sysMessagePublish = sysMessagePublish;
	}

	public List<SysMessageBean> getSysMessagePublishList() {
		return sysMessagePublishList;
	}

	public void setSysMessagePublishList(List<SysMessageBean> sysMessagePublishList) {
		this.sysMessagePublishList = sysMessagePublishList;
	}
	
	public SysMessageAction(){
		
	}

	/**
	 * 
	 * @Description:添加消息
	 * @MethodName :addMessage
	 * @Author :罗超
	 * @CreateDate :2013-7-12 上午11:32:00
	 * @return String
	 * 
	 */
	public String add() {
		definedResult = "edit";
		return "definedResult";
	}

	/**
	 * 
	 * @Description:添加消息
	 * @MethodName :addMessage
	 * @Author :罗超
	 * @CreateDate :2013-7-12 上午11:32:00
	 * @return String
	 * 
	 */
	public String del() {
		UserInfo currentUser = this.getSessionUser();
		if (currentUser == null) {
			mapResult.put("success", false);
			mapResult.put("errorMessage","当前登录用户已失效，请重新登录！");
			return "json";
		}
		try {
			sysMessagePublish = getSysMessagePublishInfoService().getById(mid);
			getSysMessagePublishInfoService().delete(sysMessagePublish);
			mapResult.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			mapResult.put("success", false);
			mapResult.put("errorMessage", e.getMessage());
		}
		
		return "json";
	}

	/**
	 * 
	 * @Description:添加消息
	 * @MethodName :addMessage
	 * @Author :罗超
	 * @CreateDate :2013-7-12 上午11:32:00
	 * @return String
	 * 
	 */
	public String save() {
		try {
			UserInfo user = this.getSessionUser();
			if (user != null) {
				userId = user.getPkId();
			} else {
				this.setMessage("当前登录用户已失效，请重新登录！");
				return ERROR;
			}
			Util u = new Util();
			SysMessagePublish sysMessagePublishs = new SysMessagePublish();
			sysMessagePublishs.setPkId(mid);
			sysMessagePublishs.setCreater(creater);
			sysMessagePublishs.setMessPublishDate(u.dateToTimestamp(null));
			sysMessagePublishs.setTitle(title);
			sysMessagePublishs.setContent(content);
			sysMessagePublishs.setCurrentUnit(currentUnit);
			sysMessagePublishs.setState(1);
			sysMessagePublishs.setUserId(user.getPkId());
			// 保存消息
			if (sysMessagePublishs.getPkId()==null||sysMessagePublishs.getPkId()==0){
				this.getSysMessagePublishInfoService().addMessage(sysMessagePublishs);
				this.setMessage("消息发布成功！");
			}else{
				getSysMessagePublishInfoService().update(sysMessagePublishs);
				this.setMessage("消息保存成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		definedResult = "sysMessage-manage";
		return "redirectAction";

	}

	/**
	 * 查询最新消息
	 * 
	 * @return　String
	 * @author zhangshuai
	 * @date 2013年7月12日 11:31:07
	 */
	public String queryMessageTOP() {
		UserInfo currentUser = this.getSessionUser();
		if (currentUser == null) {
				this.setMessage("当前用户已失效，请重新登录！");
				return ERROR;
		} else {
			userId = currentUser.getPkId();
		}
		sysMessagePublish = getSysMessagePublishInfoService().queryMessageTOP();
		sysMessagePublishList = getSysMessagePublishInfoService().getScheduleMessageList(currentUser);
		
		return SUCCESS;
	}

	/**
	 * 查询所有消息
	 * @方法	原名 querryMessageALL
	 * @return
	 */
	public String manage() {
		try {
			UserInfo user = this.getSessionUser();
			if (user == null) {
				this.setMessage("当前登录用户已失效，请重新登录！");
				return ERROR;
			}
			if (page == 0) {
				page = 1;// 当前页数
			}
			if (rowsPage == 0) {
				rowsPage = 10;// 每页显示数
			}
			if (nowpage != null && !nowpage.trim().equals("")) {
				page = Integer.parseInt(nowpage.trim());
				if (page == 0) {
					page = 1;
				}
			}
			if (maxpage != null && !maxpage.trim().equals("")) {
				rowsPage = Integer.parseInt(maxpage.trim());
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date startTime1=null; 
			Date endTime1=null;
			if(startTime!=null){
				 startTime1 = sdf.parse(startTime+" 00:00:00");
			}
			if(endTime!=null){
				 endTime1 = sdf.parse(endTime+" 23:59:59");
			}
			count = this.getSysMessagePublishInfoService().getMessageCount(startTime1, endTime1, title);// 获得总记录条数
			allPages = count % rowsPage == 0 ? count / rowsPage : count / rowsPage + 1;// 获得总页数
			result = this.getSysMessagePublishInfoService().getMessageList(page, rowsPage, allPages, startTime1, endTime1, title, count);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

	/**
	 * 查询消息详情
	 * 
	 * @return
	 */
	public String view() {
		try {
			UserInfo user = this.getSessionUser();
			if (user == null) {
				this.setMessage("当前登录用户已失效，请重新登录！");
				return ERROR;
			}
			sysMessagePublish = this.getSysMessagePublishInfoService().getById(mid);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * b编辑消息
	 * 
	 * @return
	 */
	public String edit() {
		try {
			UserInfo user = this.getSessionUser();
			if (user == null) {
				this.setMessage("当前登录用户已失效，请重新登录！");
				return ERROR;
			}
			sysMessagePublish = this.getSysMessagePublishInfoService().getById(mid);
			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String getNowpage() {
		return nowpage;
	}

	public void setNowpage(String nowpage) {
		this.nowpage = nowpage;
	}

	public String getMaxpage() {
		return maxpage;
	}

	public void setMaxpage(String maxpage) {
		this.maxpage = maxpage;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getAllPages() {
		return allPages;
	}

	public void setAllPages(int allPages) {
		this.allPages = allPages;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRowsPage() {
		return rowsPage;
	}

	public void setRowsPage(int rowsPage) {
		this.rowsPage = rowsPage;
	}

	public QueryResult<SysMessagePublish> getResult() {
		return result;
	}

	public void setResult(QueryResult<SysMessagePublish> result) {
		this.result = result;
	}

	public boolean isFirstLoginNowDay() {
		return isFirstLoginNowDay;
	}

	public void setFirstLoginNowDay(boolean isFirstLoginNowDay) {
		this.isFirstLoginNowDay = isFirstLoginNowDay;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

}
